package com.bang.study.day02.map;

/**
 * @Auther: Bang
 * @Date: 2019/9/13 23:37
 * @Description: 基于二叉搜索树实现map
 */
public class BSTMap<K extends Comparable<K>, V> implements Map<K, V> {

    private class Node {
        public K key;
        public V value;
        private Node left;
        private Node right;

        public Node() {
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public Node(K key, V value, Node left, Node right) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    private Node root;

    private int size;

    public BSTMap() {
        this.root = null;
        this.size = 0;
    }

    @Override
    public void add(K key, V value) {
        root = addNode(root, key, value);
    }

    /**
     * 向以node为根的二分搜索树中插入元素(key, value)，递归算法
     *
     * @param node  根节点
     * @param key
     * @param value
     * @return
     */
    private Node addNode(Node node, K key, V value) {
        if (null == node) {
            size++;
            return new Node(key, value);
        } else {
            K currKey = node.key;
            if (key.compareTo(currKey) < 0) {
                node.left = addNode(node.left, key, value);
            } else if (key.compareTo(currKey) > 0) {
                node.right = addNode(node.right, key, value);
            } else {
                node.value = value;
            }
            return node;
        }
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (node == null) {
            throw new IllegalArgumentException(key + " doesn't exist!");
        } else {
            node.value = newValue;
        }
    }

    @Override
    public V remove(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            root = removeNode(root, key);
            return node.value;
        }
        return null;
    }

    /**
     * 递归删除元素,返回删除的key对应得value
     *
     * @param node
     * @param key
     * @return
     */
    private Node removeNode(Node node, K key) {
        if (node == null) {
            return null;
        } else {
            K currKey = node.key;
            if (key.compareTo(currKey) < 0) {
                node.left = removeNode(node.left, key);
                return node;
            } else if (key.compareTo(currKey) > 0) {
                node.right = removeNode(node.right, key);
                return node;
            } else {
                if (node.left == null) {
                    node.left = node.right;
                    node.right = null;
                    size--;
                    return node;
                } else if (node.right == null) {
                    node.right = node.left;
                    node.left = null;
                    size--;
                    return node;
                } else {
                    Node successor = minimum(node);
                    successor.left = node.left;
                    successor.right = removeMin(node.right);
                    node.left = node.right = null;
                    return successor;
                }
            }
        }
    }

    /**
     * 返回以node为根的二分搜索树的最小值所在的节点
     *
     * @param node
     * @return
     */
    private Node minimum(Node node) {
        if (null == node) {
            return null;
        } else {
            return minimum(node.left);
        }
    }

    /**
     * 删除掉以node为根的二分搜索树中的最小节点
     *
     * @param node
     * @return 返回删除节点后新的二分搜索树的根
     */
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        } else {
            node.left = removeMin(node.left);
            return node;
        }
    }


    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            return node.value;
        }
        return null;
    }

    private Node getNode(Node node, K key) {
        if (null == node) {
            return null;
        } else {
            if (key.equals(node.key)) {
                return node;
            } else if (key.compareTo(node.key) < 0) {
                return getNode(node.left, key);
            } else {
                return getNode(node.right, key);
            }
        }
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean contains(K key) {
        Node node = getNode(root, key);
        return node!=null;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }


}
